import Mock from "mockjs";
import qs from 'qs'

Mock.setup({
  // 随机延迟500-1000毫秒
  timeout: '500-1000'
})

/**
 * 拦截请求，
 * 第一个参数：url，使用正则去匹配
 * 第二个参数：请求方式
 * 第三个参数： 生成数据的函数
 */
Mock.mock(/\/my\/test/, 'get', () => {
  const result = {
    msg: "请求测试结果成功",
    result: []
  }

  return result
})


// 模拟收藏数据
Mock.mock(/\/member\/collect/, 'get', config => {
  // 解析出query信息
  const queryString = config.url.split('?')[1]
  const queryObject = qs.parse(queryString)

  // 生成模拟数据
  const items = []
  for (let i = 0; i < queryObject.pageSize; i++) {
    items.push(Mock.mock({
      id: '@id',
      // name: '@cname',
      name: '@ctitle(5,20)',
      desc: '@ctitle(4,10)',
      price: '@float(100,200,2,2)',
      picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock('@integer(1,8)')}.jpg`
    }))
  }

  return {
    msg: '获取收藏商品成功',
    result: {
      count: 35,
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
      items
    }
  }
})

// 模拟足迹数据
Mock.mock(/\/member\/browseHistory/, 'get', (config) => {
  const queryString = config.url.split('?')[1]
  const queryObject = qs.parse(queryString)

  const items = []
  for (let i = 0; i < queryObject.pageSize; i++) {
    items.push(Mock.mock({
      id: '@id',
      name: '@ctitle(8,18)',
      desc: '@ctitle(5,12)',
      price: '@float(99,168,2,2)',
      picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock('@integer(1,8)')}.jpg`
    }))
  }

  return {
    msg: '获取足迹信息成功',
    result: {
      count: 30,
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
      items
    }
  }
})